var table;
$(function () {
    initDateRangePicker();
    loadOption();
});

function initDateRangePicker() {
    var date = new Date();
    if(date.getMonth() > 0) {
        $('input[name="daterange"]').daterangepicker({
            locale: {
                format: 'YYYY-MM-DD'
            },
            startDate: date.getFullYear() + "-" + formNumber(date.getMonth()) + formNumber(date.getDate()),
            endDate: date.getFullYear() + "-" + formNumber(date.getMonth() + 1) + '-' + formNumber(date.getDate())
        },
        function (start, end, label) {
            //                        alert("A new date range was chosen: " + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
        });
    } else {
        $('input[name="daterange"]').daterangepicker({
            locale: {
                format: 'YYYY-MM-DD'
            },
            startDate: date.getFullYear() - 1 + "-" + formNumber(date.getMonth() + 12) + formNumber(date.getDate()),
            endDate: date.getFullYear() + "-" + formNumber(date.getMonth() + 1) + '-' + formNumber(date.getDate())
        },
        function (start, end, label) {
            //                        alert("A new date range was chosen: " + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
        });
    }
}

/**
 * 加载地区下拉框数据
 */
function loadOption() {
    var url = baseRegionUrl + "list/region?start=0&limit=1000";
    ajaxCommon.request('get',url,{},function (data) {
        var arr = [];
        $(data.data.rows).each(function (index, obj) {
            var region = {"id": obj.id, "text": obj.name};
            arr.push(region);
        });
        $('#region').select2({
            data: arr
        });
        loadData();
    })
}

function loadData() {
    var regionId = $("#region").val();
    var dateInterval = $("#reservation").val().split(" - ");
    var startDate = Date.parse(dateInterval[0]);
    var endDate = Date.parse(dateInterval[1]);
    var url = baseQuestionUrl + "longroad?start=0&limit=1000000";
    ajaxCommon.request('get',url, {"search": regionId, "startDate": startDate, "endDate": endDate},function (data) {
        var array = [];
        for (var i = 0; i < data.data.rows.length; i++) {
            var circuitry = {
                "index": i + 1,
                "circuitry": data.data.rows[i].firest,
                "num": data.data.rows[i].count
            };
            array.push(circuitry);
        }
        loadTable(array);
    })
}

function loadTable(data) {
    var obj={
        data: data,
        columns: [
            {data: 'index'},
            {data: 'num'},
            {data: 'circuitry'}
        ],
    }
    ajaxCommon.loadTableData("#circuitryList",obj)
}

function loadHistogramData() {
    loadData();
}

function clearSelect() {
    var region = $("#region");
    var select = $(".select2-selection__rendered");
    region.find("option:first").prop("selected", 'selected');
    select.text(region.find("option:first").text());
    select.attr("title", region.find("option:first").text());
    initDateRangePicker();
    loadData();
}

/**
 * 导出Excel
 */
function exportExcel() {
    var regionId = $("#region").val();
    var dateInterval = $("#reservation").val().split(" - ");
    var startDate = Date.parse(dateInterval[0]);
    var endDate = Date.parse(dateInterval[1]);
    var url = baseQuestionUrl + "longroad?start=0&limit=1000000";
    ajaxCommon.request('get',url,{"search": regionId, "startDate": startDate, "endDate": endDate},function (data) {
        if (data.data.rows.length <= 0) {
            swal("当前数据为空");
            return;
        }
        var sheetData = [];
        $(data.data.rows).each(function (index, obj) {
            var date = formatDate(new Date, obj.created);
            var d = {
                "index": index + 1,
                "circuitry": obj.firest,
                "num": obj.count
            };
            sheetData.push(d);
        });
        var option = {};
        option.fileName = "问题线路(" + $("#reservation").val().split(" - ") + ")";
        option.datas = [
            {
                sheetData: sheetData,
                sheetName: 'questionCircuitry',
                sheetFilter: ['index', 'circuitry', "num"],
                sheetHeader: ['排名', '线路', '次数']
            },
            {
                sheetData: sheetData
            }
        ];
        var toExcel = new ExportJsonExcel(option);
        toExcel.saveExcel();
    })
}
